home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / basic / UDP_Chat.lha / UDPChat / Old_Stuff / UDP_Receive.asc < prev    next >
Encoding:
Text File  |  1998-08-02  |  3.1 KB  |  137 lines

  1. ;
  2. ;                 UDP receive code
  3. ;
  4. ;    This receives data on its socket (which is bound to a port
  5. ;  on localhost), from any host that sends to it.
  6. ;
  7. ;  Written by Anton Reinauer <anton@ww.co.nz>.
  8. ;
  9. ;  Thanks to Paul Burkey for TCP_Funcs and to Dr. Ercole Spiteri
  10. ;  for TCP-to-Blitz.
  11. ;
  12.  
  13.  
  14. WBStartup
  15. NoCli
  16. Hostname.s="localhost"   ; you need to bind socket to localhost
  17. #PORT=3001               ; a free port to bind to- it can't
  18.                          ; be a port used in Services.
  19.  
  20. INCLUDE "TCPFuncs.bb"
  21. DEFTYPE .w
  22.  
  23. ;********************************************************
  24.  
  25. .ReadUDP
  26. Function .s ReadUDP{}
  27.   SHARED sock.l,TCPmem.l
  28.  
  29.   ; This Function reads data from the socket it is bound to.
  30.   ; If there is no messages then it will return an empty string =""
  31.  
  32.   sockread.l=0                              ;Clear Readmask
  33.   sockread.l BitSet sock.l                  ;Set Readmask on our socket
  34.   e=IoctlSocket_(sock.l,#FIONREAD,TCPmem.l) ;How much data is there?
  35.   f.l=Peek.l(TCPmem.l)                      ;Place value in f
  36.  
  37.   If f>0                                    ; any data waiting
  38.  
  39.     c=recvfrom_(sock.l,TCPmem.l,f,0,0,messagelen)  ;Read all data waiting on socket
  40.  
  41.     If c>0    ; if there any data waiting
  42.         a=0 : c$=""
  43.         Repeat
  44.             c$=c$+Chr$(Peek.b(TCPmem+a)) : a+1    ; build string
  45.         Until a=c
  46.     EndIf
  47.  
  48.   EndIf
  49.  
  50.   Function Return c$
  51. End Function
  52.  
  53. .ConnectUDP2
  54. Function  ConnectUDP2{host$,port.w}
  55.   SHARED sock.l,host.sockaddrin,hostlen.w
  56.   ;
  57.   ; Connect to host at specified port
  58.   ; Return true or False if Connection is made
  59.   ;
  60.  
  61.   sock.l=socket_(2,2,0)        ; open UDP socket
  62.  
  63.   *a.hostent=gethostbyname_(host$)   ; get local host structure
  64.  
  65.   ;Copy Details to our Sockaddrin structure
  66.  
  67.   bb=CopyMem_(*a.hostent\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a.hostent\h_length)
  68.  
  69.   host.sockaddrin\sin_port=port       ;Set local port number
  70.   host.sockaddrin\sin_family=2        ;Set type to AT_INET
  71.   hostlen=SizeOf.sockaddrin           ;Get lenght of structure sockaddrin
  72.  
  73.   If bind_(sock,host,hostlen)=0    ; bind socket to port so we can
  74.     Function Return True           ; receive data on port
  75.   EndIf
  76.  
  77. End Function
  78.  
  79. ;****************************************
  80.  
  81.  
  82. WbToScreen0
  83. Window 0,300,100,320,150,$1|$2|$4|$8|$400,"Receive UDP",1,2
  84.  
  85. WindowOutput0
  86. WindowInput 0
  87.  
  88. If ConnectUDP2 {Hostname,#PORT}              ; bind socket to local port
  89.    NPrint "Bound to ",Hostname," ",#PORT
  90. Else
  91.    NPrint "Can't bind to ",Hostname," ",#PORT
  92.    Goto Exit
  93. EndIf
  94.  
  95. ypos=10
  96.  
  97. ;****************************************
  98.  
  99. .Main
  100.  
  101. Repeat
  102.     Delay_(1)
  103.  
  104.     a$=ReadUDP{}   ; get data from socket
  105.  
  106.     If a$<>""
  107.         t$=a$
  108.         Gosub Print_String
  109.     EndIf
  110.  
  111.     ev.l=Event
  112. Until ev=$200    ; shut down if close gadget hit
  113.  
  114.  
  115. CloseTCP{}                          ; close connection
  116.  
  117. WLocate 10,10
  118. Print "Connection  closed"
  119.  
  120. Exit:
  121. Delay_(50)
  122. FreeMem TCPmem,$2000
  123.  
  124. End
  125.  
  126. ;***********************  Gosubs *********************
  127.  
  128. .Print_String
  129.       ypos+10
  130.       If ypos=130 Then ypos=20
  131.       WLocate 10,ypos
  132.       Print "                                             "
  133.       WLocate 10,ypos
  134.       Print "RECEIVED: ",t$               ; print string received
  135. Return
  136.  
  137.